<template>
  <div class="register">
    <span>———————— &nbsp;&nbsp; 注册账号 &nbsp;&nbsp;————————</span>
    <div class="center">
      <el-form
        ref="formInstance"
        :model="form"
        label-width="auto"
        status-icon
        style="max-width: 600px"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="输入用户名(4-10位数字或字母组成)"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            show-password
            placeholder="输入登录密码(必须等于6位)"
          />
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item
          prop="code"
          :rules="[
            { required: true, message: '请输入验证码', trigger: 'blur' },
          ]"
        >
          <div style="width: 50%">
            <el-input type="text" placeholder="验证码" v-model="form.code" />
            <i class="countdown"></i>
          </div>
          <div class="btnCode">
            <el-button type="primary" @click="getCodeFun">获取验证码</el-button>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="SubmitForm(formInstance)"
            >注册</el-button
          >
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
    <span>——————————————————————</span>
    <span class="toLogin" @click="toLogin">已有账号？直接登录>></span>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance, FormRules } from "element-plus";
import { getCodeApi, registerApi } from "./api/user";
import { ElMessage } from "element-plus";

const router = useRouter();
const formInstance = ref<FormInstance>();

const form = reactive({
  username: "",
  password: "",
  phone: "",
  code: "",
});

const checkPhone = (rule: any, value: any, callback: any) => {
  if (!/^1[345789]\d{9}$/.test(value)) {
    console.log("手机号码格式不正确");
    return callback(new Error("手机号码格式不正确"));
  }
  return callback();
};

const rules = reactive<FormRules<typeof form>>({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      max: 10,
      min: 4,
      message: "用户名由4-10位数字或字母组成",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { max: 6, min: 6, message: "密码必须等于6位数", trigger: "blur" },
  ],
  phone: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    { validator: checkPhone, trigger: "blur" },
  ],
});

const SubmitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    console.log(valid);
    if (valid) {
      // 请求
      registerApi(form).then((res) => {
        console.log(res);
        if (res.success) {
          // 跳转
          router.push("/login");

          // 提示
          ElMessage({
            message: "注册成功，请登录",
            type: "success",
          });
        }
      });
    }
  });
};

const getCodeFun = () => {
  if (/^1[345789]\d{9}$/.test(form.phone)) {
    getCodeApi(form.phone);
  }
};

const toLogin = () => {
  router.push("/login");
};
</script>

<style lang="less" scoped>
.register {
  width: 400px;
  margin: 100px auto;
  padding: 20px;
  text-align: center;
  border: 1px solid #ffffff99;
  .center {
    width: 300px;
    margin: 20px auto 0;
  }
  span {
    color: #ffffff99;
    display: inline-block;
  }
  .toLogin {
    font-size: 12px;
    cursor: pointer;
    color: #409eff;
  }
  .btnCode {
    width: 35%;
    margin-left: auto;
  }
}
</style>